<script>
import { mapState } from 'vuex'
import SearchManage from '@/components/basic/SearchManage.vue'

import { formConfig, column, config, formatData } from './formConfig.js'
import { requestApi } from '@/requset/api'
export default {
    extends: SearchManage,
    data() {
        return {
            table_title: '能耗',
            isShow: true,
            troubleList: [],
            url: 'energy',
            formConfig: null,
            tableColumn: null,
            tableData: [],
            baseForm: {
                level: [
                    {
                        level1: '≤',
                        level2: '~',
                        level3: '~',
                    },
                ],
            },
            queryForm: {
                scope_id: null,
                scope_id_list: [],
                page: 1,
                size: 20,
            },
            queryFormC: {
                scope_id: null,
                scope_id_list: [],
                page: 1,
                size: 20,
            },
            actions: [
                {
                    label: '导出',
                    icon: 'el-icon-download',
                    click: () => {
                        this.download()
                    },
                    if: () => this.operate,
                },
                {
                    label: '添加',
                    icon: 'el-icon-folder-add',
                    click: () => {
                        this.handleAdd()
                    },
                    if: () => this.operate,
                },
            ],
        }
    },
    methods: {
        to(data) {
            data.level = JSON.stringify(data.level)
            return data
        },
        getlist() {
            this.loading = true
            requestApi('post', this.url + `/getlist`, this.queryForm)
                .then((res) => {
                    res.data.data.map((item) => {
                        try {
                            item.level = JSON.parse(item.level.replace(/NaN/g, '""'))
                        } catch (error) {
                            console.log(error)
                        }
                    })
                    this.tableData = res.data.data
                    this.pagination = res.data.pagination
                    if (res.data.stat) {
                        this.stat = res.data.stat
                    }
                })
                .finally(() => {
                    this.loading = false
                })
        },
    },

    computed: {
        ...mapState('backstage', ['backstageData']),
    },
    created() {
        this.formConfig = formConfig
        this.dialog.config = config
        this.dialog.config[0].onChange = (config, form) => {
            if (form.type === 'wind') {
                form.name = '通风系统工序能耗'
                form.unit = 'kWh/(Mm3·Pa)'
                form.level = [
                    {
                        level1: '≤0.360',
                        level2: '0.361~0.400',
                        level3: '0.401~0.550',
                    },
                ]
                config[5].show = () => true
                config[6].show = () => false
                config[7].show = () => false
                config[8].show = () => false
            }
            if (form.type === 'water') {
                form.name = '排水系统工序能耗'
                form.unit = 'kWh/(t·hm)'
                form.level = [
                    {
                        level1: '≤0.390',
                        level2: '0.391~0.440',
                        level3: '0.441~0.500',
                    },
                ]
                config[5].show = () => false
                config[6].show = () => true
                config[7].show = () => false
                config[8].show = () => false
            }
            if (form.type === 'up') {
                form.name = '提升机系统工序能耗'
                form.unit = 'kWh/(t·hm)'
                form.level = [
                    {
                        level1: '≤0.500',
                        level2: '0.501~0.580',
                        level3: '0.581~0.680',
                    },
                ]
                config[5].show = () => false
                config[6].show = () => false
                config[7].show = () => true
                config[8].show = () => false
            }
            if (form.type === 'air') {
                form.name = '空气压缩系统工序能耗'
                form.unit = 'kWh/(m3·MPa)'
                form.level = [
                    {
                        level1: '≤0.107',
                        level2: '0.108~0.114',
                        level3: '0.115~0.130',
                    },
                ]
                config[5].show = () => false
                config[6].show = () => false
                config[7].show = () => false
                config[8].show = () => true
            }
        }
        this.dialog.labelWidth = '180px'
        column[column.length - 1].buttons = [
            {
                text: '编辑',
                click: (val) => this.handleEdit(val),
                if: () => this.operate,
                icon: 'el-icon-edit',
            },
            {
                text: '删除',
                click: (val) => this.delete(val),
                if: () => this.operate,
                icon: 'el-icon-delete',
            },
        ]
        this.tableColumn = column
        this.searchBtn()
    },
}
</script>
<style>
.el-row {
    margin-bottom: 20px;

    &:last-child {
        margin-bottom: 0;
    }
}
</style>
